<template>
  <div class="userManager-box">
    <el-row class="btn-group">
      <el-col :span="9"><el-input v-model="reach" prefix-icon="el-icon-search" placeholder="请输入搜索的用户"></el-input><el-button type="primary" @click="reachUser">搜索</el-button></el-col>
      <el-col :span="4"><el-button type="danger" @click="bathdeleteOrder">批量删除</el-button></el-col>
    </el-row>
    <el-row class="userManager-content">
      <el-table :data="orderlist" @selection-change="getIds">
        <el-table-column type="selection"></el-table-column>
        <el-table-column width="110px">
          <template slot-scope="scope">
            <img :src="scope.row.shoes_img" class="shoes-img"/>
          </template>
        </el-table-column>
        <el-table-column label="商品名称" prop="shoes_name" width="100px" align="center"></el-table-column>
        <el-table-column label="用户名" prop="user_name" width="100px" align="center"></el-table-column>
        <el-table-column label="收货人姓名" prop="name" width="100px" align="center"></el-table-column>
        <el-table-column label="手机号码" prop="address_phone" width="140px" align="center"></el-table-column>
        <el-table-column label="商品单价" prop="price" align="center"></el-table-column>
        <el-table-column label="购买数量" prop="shoes_num" align="center"></el-table-column>
        <el-table-column label="收货地址" width="220px" align="center">
          <template slot-scope="scope">
            <span>{{scope.row.address_name}}-{{scope.row.detailed_address}}</span>
          </template>
        </el-table-column>
        <el-table-column label="状态" width="200px" align="center">
          <template slot-scope="scope">
            <el-switch active-text="已通过" inactive-text="未通过" :active-value="1" :inactive-value="0" v-model="orderlist[scope.$index].state" @click.native="tiggerState(scope.row.order_id,scope.row.state)"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="text" @click="deleteOrder(scope.row.order_id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
    <el-row type="flex" justify="center">
      <el-pagination
            @size-change="pageSizeChange"
            @current-change="currPageChange"
            :current-page="curr_page"
            :page-sizes="[10, 50, 100, 500, 1000]"
            :page-size="page_size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
          </el-pagination>
    </el-row>
  </div>
</template>
<script>
import http from '@/utils/HttpUtil'
export default {
  data () {
    return {
      reach: '',
      dialogVisible: false,
      action: {
        selectPageOrder: 'shoes_Web_exploded/Order/selectPageOrder',
        updataOrderState: 'shoes_Web_exploded/Order/updateOrderState',
        deletedOrder: 'shoes_Web_exploded/Order/deletedOrder'
      },
      orderlist: [],
      curr_page: 1,
      page_size: 10,
      total: 0,
      ids: ''
    }
  },
  components: {

  },
  computed: {

  },
  methods: {
    reachUser () {
      this.getOrderList(this.reach, 1, this.pageSize)
    },
    pageSizeChange (pageSize) {
      this.getOrderList(this.reach, 1, pageSize)
    },
    currPageChange (currPage) {
      this.getOrderList(this.reach, currPage, this.page_size)
    },
    getOrderList (reach = null, curr_page = 1, page_size = 10) {
      const params = {
        'curr_page': curr_page,
        'page_size': page_size
      }
      if (reach !== null && reach !== '') {
        params['user_name'] = reach
      }
      http.xhrPost(this, this.action.selectPageOrder, params, (res) => {
        this.orderlist = res.data.list
        this.curr_page = res.data.pageNum
        this.page_size = res.data.page_size
        this.total = res.data.total
      })
    },
    tiggerState (orderId, state) {
      let params = {
        'state': state
      }
      http.xhrPost(this, this.action.updataOrderState + '/' + orderId, params, (res) => {
        this.$message({
          type: 'success',
          message: res.message
        })
      })
    },
    getIds (orderlist) {
      // 清楚上次选中的id
      this.ids = ''
      orderlist.forEach((item, index) => {
        this.ids += item.order_id + ','
      })
    },
    bathdeleteOrder () {
      if (this.ids !== '') {
        this.$confirm('您正在执行批量删除操作', '提示', {
          confirmButtonText: '残忍丢弃',
          cancelButtonText: '容我想想',
          type: 'warning'
        }).then(() => {
          let Ids = this.ids.substring(0, this.ids.length - 1)
          this.deleted(Ids)
        })
      } else {
        this.$message({
          type: 'error',
          message: '请至少勾选一条数据'
        })
      }
    },
    deleteOrder (id) {
      http.xhrPost(this, this.action.deletedOrder + '/' + id, null, (res) => {
        this.$message({
          type: 'success',
          message: res.message
        })
        this.getOrderList(this.name, this.curr_page, this.page_size)
      }, 'delete')
    },
    deleted (Ids) {
      http.xhrPost(this, this.action.deletedOrder + '/' + Ids, null, (res) => {
        this.$message({
          type: 'success',
          message: res.message
        })
        this.getOrderList(this.name, this.curr_page, this.page_size)
      }, 'delete')
    }
  },
  mounted () {
    this.getOrderList()
  }
}
</script>

<style lang="scss">
@import '../../assets/style/common.scss';
  .userManager-box {
    margin: 0px;
    padding: 0px;
    .btn-group {
      .el-input {
        width: 250px;
      }
    }
    .userManager-content {
      margin-top: 20px;
      .shoes-img {
        width: 100px;
        height: 100px;
      }
    }
  }
</style>
